import os
from os.path import join, exists
from uuid import uuid4

from Autotests.BaseTest import AirClientTestCase
from Autotests.LocalMachine.Pages.AirClient.BackupPage import WebBackupPage, BackupPlugins
from Autotests.LocalMachine.Pages.AirClient.RestorePage import WebRestorePage
from Common.Helpers import FileSystemHelper


class Backup(AirClientTestCase):
    def setUp(self):
        super(Backup, self).setUp()
        self.fileName = "TEST_FILE_FOR_BACKUP.txt"
        self.fileForBackup = join(os.getcwd(), self.fileName)
        self.backupFileContent = 1024 * 1024 * str(uuid4())
        with open(self.fileForBackup, 'wb') as f:
            f.write(self.backupFileContent)
        self.pathForRestore = join(os.getcwd(), "TEST_FOLDER")

    def test_SimpleBackup(self):
        self.backupAndRestore()

    def test_SimpleBackupWithPath(self):
        self.backupAndRestore(restoreToPath=self.pathForRestore)

    def test_BackupAndSearch(self):
        self.backupAndRestore(search=True)

    def test_BackupAndSearchWithPath(self):
        self.backupAndRestore(search=True, restoreToPath=self.pathForRestore)

    def backupAndRestore(self, search=False, restoreToPath=None):
        WebBackupPage().performBackup(self.fileForBackup, BackupPlugins.filesystem)
        os.remove(self.fileForBackup)
        self.assertTrue(not exists(self.fileForBackup), "file hasn't been removed")
        if restoreToPath:
            FileSystemHelper().recreateFolder(restoreToPath)
            expectedRestorePath = join(restoreToPath, self.fileForBackup.replace(':', ''))
            self.performRestore(search, restoreToPath)
            self.assertRestoreIsCorrect(expectedRestorePath)
        else:
            self.performRestore(search, restoreToPath)
            self.assertRestoreIsCorrect(self.fileForBackup)

    def performRestore(self, search, restoreToPath):
        if search:
            WebRestorePage().findAndRestore(self.fileName, restoreToPath)
        else:
            WebRestorePage().performRestore(self.fileForBackup, BackupPlugins.filesystem, restoreToPath=restoreToPath)

    def assertRestoreIsCorrect(self, filePath):
        self.assertTrue(exists(filePath), "file hasn't been restored")
        with open(filePath, 'r') as f:
            self.assertEqual(f.read(), self.backupFileContent, "restored content not equal")

    def tearDownImpl(self):
        if exists(self.fileForBackup):
            os.remove(self.fileForBackup)
        FileSystemHelper().deleteFolder(self.pathForRestore)


class BackupSetup(AirClientTestCase):
    def setUp(self):
        super(BackupSetup, self).setUp()
        self.backup = WebBackupPage()

    def test_Setup(self):
        for plugin in [BackupPlugins.filesystem, BackupPlugins.vssExchange, BackupPlugins.networkShares,
                       BackupPlugins.vssMsSql, BackupPlugins.mySql]:
            self.pluginCheck(plugin)

    def pluginCheck(self, plugin):
        self.assertTrue(self.backup.selectPlugin(plugin),
                        "%s dialog hasn't been shown after enabling checkbox" % plugin)
        self.assertTrue(self.backup.isPluginEnabled(plugin), "Plugin %s hasn't been enabled" % plugin)
        self.assertTrue(self.backup.selectPluginByIconClick(plugin),
                        "%s dialog hasn't been opened after icon clicking" % plugin)
        self.assertTrue(self.backup.isPluginEnabled(plugin), "Plugin %s hasn't been enabled" % plugin)

import unittest
if __name__ == "__main__":
    unittest.main()
